package edu.wtbu.cs.androiddemo.activity.list;

import android.os.Bundle;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.wtbu.cs.androiddemo.R;

public class GridViewSampleActivity extends AppCompatActivity {
    private GridView gridView;
    private List<Map<String, Object>> datas = new
            ArrayList<Map<String, Object>>();
    private int[] icons = {R.drawable.a, R.drawable.b, R
            .drawable.c, R.drawable.d, R.drawable.e,
            R.drawable.f, R.drawable.g, R.drawable.h, R
            .drawable.i, R
            .drawable.j, R.drawable.k, R.drawable.l};
    private String[] titles = {"白鹃梅", "五色椒", "小檗",
            "李叶绣菊", "多花兰", "獐耳细辛", "铁茉莉", "三色狸藻", "东洋菊",
            "大花皇冠", "苹果花", "三弄芙蓉"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_grid_view_sample);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        gridView = (GridView) findViewById(R.id.gridView);
        initData();
        SimpleAdapter simpleAdapter = new SimpleAdapter
                (this, datas, R.layout.grid_item, new
                        String[]{"image", "titles"}, new int[]{R
                        .id.image, R.id.tv_title});
        gridView.setAdapter(simpleAdapter);
    }
    private void initData() {
        for (int i = 0; i < titles.length; i++) {
            Map<String, Object> item = new
                    HashMap<String, Object>();
            item.put("titles", titles[i]);
            item.put("image", icons[i]);
            datas.add(item);
        }
    }
}